package org.unisannio.sc;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.BaseTypes;
import android.provider.ContactsContract.CommonDataKinds.Phone;

public class ContactService {

	
	public void shareContact(Cursor cursor,ContentResolver cr){
//		long rawContactId = ContentUris.parseId(uriContact); 
		ContentValues values=new ContentValues();
		values.put(Phone.LABEL, "shared");
		
		values.put(Phone.MIMETYPE, BaseTypes.TYPE_CUSTOM);
		values.put(Phone.RAW_CONTACT_ID, cursor.getString(cursor.getColumnIndex(Phone.RAW_CONTACT_ID)));
		Uri uri=Uri.withAppendedPath(Data.CONTENT_URI,cursor.getString(cursor.getColumnIndex(Data._ID)));
		cr.update(uri, values,null,null);
	}
	
	public void hideContact(Cursor cursor,ContentResolver cr){
		ContentValues values=new ContentValues();
		values.put(Phone.LABEL, "hide");
		values.put(Phone.MIMETYPE, BaseTypes.TYPE_CUSTOM);
		values.put(Phone.RAW_CONTACT_ID, cursor.getString(cursor.getColumnIndex(Phone.RAW_CONTACT_ID)));
		Uri uri=Uri.withAppendedPath(Data.CONTENT_URI,cursor.getString(cursor.getColumnIndex(Data._ID)));
		cr.update(uri, values,null,null);
	}
	
	
	public String getGroup(ContentResolver cr,Uri uri){
		Cursor cursor=cr.query(uri, null, null, null, null);
		cursor.getColumnNames();
		cursor.moveToFirst();
		int i=cursor.getColumnIndex(Phone.LABEL);
		if(i==-1)
			return "no share";
		return cursor.getString(i);
	}
	
	public List<Contact> getContactsList(Cursor cursor){
		ArrayList<Contact> contacts=new ArrayList<Contact>();
		
		if (cursor!=null) {
			cursor.moveToFirst();
			Contact current;
			while (!cursor.isAfterLast()) {
				current = new Contact();
				current.setId(cursor.getString(cursor.getColumnIndex(Data._ID)));
				if (cursor.getString(cursor.getColumnIndex(Phone.LABEL)) == null) {
					current.setLabel("hide");
				} else {
					current.setLabel(cursor.getString(cursor
							.getColumnIndex(Phone.LABEL)));
				}

				current.setName(cursor.getString(cursor
						.getColumnIndex(Data.DISPLAY_NAME)));
				current.setNumber(cursor.getString(cursor
						.getColumnIndex(Phone.NUMBER)));
				current.setRawContactId(cursor.getString(cursor
						.getColumnIndex(Phone.RAW_CONTACT_ID)));
				contacts.add(current);
				cursor.moveToNext();
			}
		}
		
		return contacts;
	}
	
}
